package cn.edu.abtu.service.impl;

import cn.edu.abtu.exceptionhandler.GuliException;
import cn.edu.abtu.service.VodService;
import cn.edu.abtu.utils.ConstantPropertiesUtils;
import cn.edu.abtu.utils.InitVodClient;
import cn.edu.abtu.vo.ResponseData;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {

    @Override
    public ResponseData uploadVideo(MultipartFile file) {
        try {
            //返回视频上传的ID值
            String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
            String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
            String title = file.getOriginalFilename(); //上传后显示的名称
            String fileName = file.getOriginalFilename(); //上传文件原始名称
            InputStream inputStream = file.getInputStream();
            //上传视频的方法
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId,accessKeySecret,title,fileName,inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()){
                videoId = response.getVideoId();
                return ResponseData.success(fileName+"上传成功!",videoId);
            }else {
                videoId = response.getVideoId();
                return ResponseData.success(fileName+"上传失败!",videoId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseData removeVideo(String videoId) {
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频ID
            request.setVideoIds(videoId);
            //调用初始化的方法实现删除
            client.getAcsResponse(request);
            return ResponseData.success("视频删除成功!",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.fail("视频删除失败!",null);
        }
    }

    @Override
    public ResponseData deleteBatch(List<String> videoList) {
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //videoList转换为1,2,3,4
            String videoIds =  StringUtils.join(videoList.toArray(),",");
            //向request设置视频ID
            request.setVideoIds(videoIds);
            //调用初始化的方法实现删除
            client.getAcsResponse(request);
            return ResponseData.success("视频删除成功!",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.fail("视频删除失败!",null);
        }
    }

    @Override
    public ResponseData getPlayAuth(String videoId) {
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request中设置视频ID
            request.setVideoId(videoId);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return ResponseData.success(playAuth);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.fail();
        }
    }

}
